package psk.lightshow;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class ModelText extends Activity {
	private Activity nowActivity = ModelText.this;
	private TextView tvModelText,tvFlashTime,tvIntervalTime,tvAllTime;
	private EditText etText;
	private int r=255,g=255,b=255,textLength=0,flash=100,interval=250;
	private int showModel = 1;
	private Bitmap bitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.model_text);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.head_back_title);
		initHead();
		initView();
	}
	private void initHead(){
		TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
        tvTitle.setText("文本");
        Button btnBack = (Button)findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		nowActivity.finish();
        	}
        });
	}
	private void initView(){
		tvAllTime = (TextView)findViewById(R.id.tvAllTime);
		tvModelText = (TextView)findViewById(R.id.tvModelText);
		tvFlashTime = (TextView)findViewById(R.id.tvFlashTime);
		tvIntervalTime = (TextView)findViewById(R.id.tvIntervalTime);
		BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.drawable.icon_color_long);
		bitmap = bd.getBitmap();
		Intent it = this.getIntent();
		Bundle bl = it.getExtras();
		changeColor(bl.getInt("position"));
		//Initial the etText and set listener to listen the change of etText---------------------------
		etText = (EditText)findViewById(R.id.etText);
		etText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				textLength = etText.getText().toString().length();
				showAllTime(textLength, flash, interval);
			}
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			public void afterTextChanged(Editable s) {
				
			}
		});
		//Set the seek bar to change the text's color------------------------------------------------
		SeekBar sbChangeColor = (SeekBar)findViewById(R.id.sbChangeColor);
		sbChangeColor.setMax(bitmap.getWidth()-1);
		sbChangeColor.setProgress(bl.getInt("position"));
		sbChangeColor.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				changeColor(progress);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		//Set the seek bar to change the show time----------------------------------------------------
		SeekBar sbFlashTime = (SeekBar)findViewById(R.id.sbFlashTime);
		sbFlashTime.setMax(24);
		sbFlashTime.setProgress(4);
		sbFlashTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				flash = (progress+1)*20;
				double f = (double)flash/(double)1000;
				tvFlashTime.setText(String.format("%.2f", f)+"s");
				showAllTime(textLength,flash,interval);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		//Set the seek bar to change the hide time------------------------------------------------------
		SeekBar sbIntervalTime = (SeekBar)findViewById(R.id.sbIntervalTime);
		sbIntervalTime.setMax(19);
		sbIntervalTime.setProgress(4);
		sbIntervalTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				interval = (progress+1)*50;
				double f = (double)interval/(double)1000;
				tvIntervalTime.setText(String.format("%.2f", f)+"s");
				showAllTime(textLength,flash,interval);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		//Set the button of btnShowModel to change the model of the show-------------------------------
		final Button btnShowModel = (Button)findViewById(R.id.btnShowModel);
		btnShowModel.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				if(btnShowModel.getText().toString().equals("单次")){
					btnShowModel.setText("循环");
					showModel = 0;
				}else{
					btnShowModel.setText("单次");
					showModel = 1;
				}
			}
		});
		//Set the button of btnToShow to start show activity---------------------------------------
		Button btnToShow = (Button)findViewById(R.id.btnToShow);
		btnToShow.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				if(etText.getText().toString().equals("")){
					Toast.makeText(nowActivity, "请输入文本！", Toast.LENGTH_SHORT).show();
					return;
				}
				Intent it = new Intent(nowActivity,Show.class);
				Bundle bl = new Bundle();
				bl.putString("type", "text");
				bl.putString("content", etText.getText().toString());
				bl.putInt("model", showModel);
				bl.putInt("flash", flash);
				bl.putInt("interval", interval);
				bl.putInt("r", r);
				bl.putInt("g", g);
				bl.putInt("b", b);
				it.putExtras(bl);
				startActivity(it);
			}
		});
	}
	/**
	 * Change the total time of the show--------------------------------------------------
	 * @param textLength-> the text length
	 * @param flash-> the time of show with millisecond
	 * @param interval-> the time of hide with millisecond
	 */
	private void showAllTime(int textLength,int flash,int interval){
		double time_s = (double)(textLength*(flash+interval)-interval)/(double)1000;
		String time = String.format("%.2f", time_s);
		tvAllTime.setText("共耗时"+time+" s");
	}
	/**
	 * Change the color of text---------------------------------------------------------------
	 * @param progress-> the position of seek bar
	 */
	private void changeColor(int progress){
		int pixel = bitmap.getPixel(progress, 5);
		r = Color.red(pixel);
		g = Color.green(pixel);
		b = Color.blue(pixel);
		tvModelText.setTextColor(Color.rgb(r, g, b));
	}

}
